package org.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.example.domain.TJrProduct;
import org.example.enums.InstalmentPeriodTypeEnum;
import org.example.enums.RepaymentTypeEnum;
import org.example.service.TJrProductService;
import org.example.mapper.TJrProductMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 任众
* @description 针对表【t_jr_product(金融分期列表)】的数据库操作Service实现
* @createDate 2023-04-24 20:25:00
*/
@Service
public class TJrProductServiceImpl extends ServiceImpl<TJrProductMapper, TJrProduct>
    implements TJrProductService{

    @Override

    public List<TJrProduct> getProductInstallment(Integer num) {
        QueryWrapper<TJrProduct> wrapper = new QueryWrapper<>();
        //根据金融产品类型分类查询
        wrapper.eq("jr_product_type",num);
        List<TJrProduct> list = this.list(wrapper);
        ArrayList<TJrProduct> products = new ArrayList<>();
        StringBuilder accountInformation = null;
        for (TJrProduct product : list) {
            accountInformation = new StringBuilder();
            //如银行信息不为空进行拼接
            if(StrUtil.isNotEmpty(product.getBankName())) {
                accountInformation.append(product.getBankName());
            }
            //如银行卡号信息不为空进行拼接
            if (StrUtil.isNotEmpty(product.getBankCardNumber())) {
                accountInformation.append(product.getBankCardNumber());
            }

            if(product.getInstalmentPeriod() == 11) {
                product.setInstalmentPeriodName("11个月");
            }else if(product.getInstalmentPeriod() == 23) {
                product.setInstalmentPeriodName("23个月");
            }else {
                //还款时间
                product.setInstalmentPeriodName(InstalmentPeriodTypeEnum.getName(product.getInstalmentPeriod().intValue()));
            }
            //还款方式
            product.setRepaymentTypeName(RepaymentTypeEnum.getName(product.getRepaymentType()));
            //银行卡
            product.setAccountInformation(accountInformation.toString());

            products.add(product);
        }
        return products;
    }

    @Override
    public AjaxResult saveOrUpdates(TJrProduct tJrProduct) {
        if (tJrProduct.getJrProductType()!=null){
            //判断金融产品类型
            if (tJrProduct.getJrProductType()==1){
                //金融产品类型,"1：租房分期2：收房贷3：应收租约保理"
                //判断分期期限不为null，如为null就保存BindingResult中收集错误信息，并将其传递给视图层，以便在用户界面上显示错误信息
                //判断分期期限
                if (tJrProduct.getInstalmentPeriod() == null || tJrProduct.getInstalmentPeriod() == 0){
                    return AjaxResult.error("instalmentPeriod","{common.jrProduct.byStages}");
                }
            }else if (tJrProduct.getJrProductType()==2){
                if (tJrProduct.getInstalmentPeriod() == null || tJrProduct.getInstalmentPeriod() == 0){
                    return AjaxResult.error("instalmentPeriod","{common.jrProduct.mortgageLoan}");
                }
            }else if (tJrProduct.getJrProductType()==3){
                if (tJrProduct.getInstalmentPeriod() == null || tJrProduct.getInstalmentPeriod() == 0){
                    return AjaxResult.error("instalmentPeriod","{common.jrProduct.leaseFactoring}");
                }
            }

            //判断是修改还是添加
            QueryWrapper<TJrProduct> wrapper = new QueryWrapper<>();
            wrapper.eq("del_flg",0);
            wrapper.eq("jr_product_name",tJrProduct.getJrProductName());
            TJrProduct one = this.getOne(wrapper);

            if (ObjectUtil.isNull(one)){
                tJrProduct.setCreateUserid(SecurityUtils.getUserId());
                tJrProduct.setCreateTime(new Date());
                tJrProduct.setUpdateTime(new Date());
                tJrProduct.setUpdateUserid(SecurityUtils.getUserId());
                boolean b = this.save(tJrProduct);
                if (b){
                    return AjaxResult.success("添加成功");
                }else{
                    return AjaxResult.error("添加失败");
                }
            }else {
                tJrProduct.setUpdateTime(new Date());

                tJrProduct.setUpdateUserid(SecurityUtils.getUserId());
                boolean b = this.saveOrUpdate(tJrProduct);
                if (b){
                    return AjaxResult.success("修改成功");
                }else{
                    return AjaxResult.error("修改失败");
                }
            }
        }
        return AjaxResult.error("产品类型不可为空");
    }

    @Override
    public AjaxResult softRemove(Integer id) {
        TJrProduct one = this.getById(id);
        if(one.getDelFlg() == 1) {
            return AjaxResult.error("该产品已作废");
        }
        one.setDelFlg(1);
        one.setUpdateTime(new Date());
        return this.saveOrUpdate(one)?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }
}




